/*
	File:				Player.cpp
	Course:		SGP 1208
	Author:		Kori Po
	Purpose:	Player object class
*/

#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"

#include "Messages\CreateAbilityMessage.h"
#include "Messages\AttackMessage.h"

#include "Player.h"
#include "Game.h"
#include "Item.h"
#include "Event.h"
#include "EventSystem.h"
#include "MessageSystem.h"
#include "Camera.h"

#include "Ability.h"

CPlayer::CPlayer(void)
{
	// Type Def
	CEntity::m_nType = OBJ_PLAYER;

	// Entry Assignment
	m_pAbilityA = NULL;
	m_pAbilityB = NULL;
	m_nHP = 100;
	m_nMP = 100;
	m_nHPRegen = 2;
	m_nMPRegen = 10;
	m_nDefense = 0;
	m_nAtkPower = 20;
	m_fAtkSpeed = 1.00;
	m_nAP = 0;
	m_fCDR = 0;
	m_fMovement = 100;
	m_bUBlu = true;

	m_nMaxHP = 100;
	m_nMaxMP = 100;



	

	// Wrapper Alloc
	m_InputGuy				= CSGD_DirectInput::GetInstance();
	m_AudioGuy				= CSGD_XAudio2::GetInstance();
	m_TextureGuy			= CSGD_TextureManager::GetInstance();
	m_Messenger				= CMessageSystem::GetInstance();

	// Asset Alloc
	CEntity::SetImageID(m_TextureGuy->LoadTexture(_T("Resource/TestBox.png")));

	m_nAtttackSFXID				= m_AudioGuy->SFXLoadSound(_T("Resource/TempSFX.wav"));

	CEventSystem::GetInstance()->ClientResignation (  "Go Blue" , this );
	CEventSystem::GetInstance()->ClientResignation (  "Go Red" , this );
}

CPlayer::~CPlayer(void)
{
	if(m_pAbilityA)
		delete m_pAbilityA;

	if(m_pAbilityB)
		delete m_pAbilityB;

	CEventSystem::GetInstance()->UnregisterAll( this );
}

void CPlayer::Update(float pElapsedTime)
{
	CGame* pGame = CGame::GetInstance();

	CEntity::Update(pElapsedTime);

	Input();

	float Left = float(GetX()) - float(GetWidth()/2);
	float Top = float(GetY()) - float(GetHeight()/2);
	float Right = float(GetX()) + float(GetWidth()/2);
	float Bottom = float(GetY()) + float(GetHeight()/2);


	if( Left < 0 )
	{
		SetX( float(GetWidth()/2) ) ;
		SetVelX( 0.0f );
	}
	else if( Right > pGame->GetScreenWidth())
	{
		SetX( float(pGame->GetScreenWidth() - float(GetWidth()/2)));
		SetVelX( 0.0f );
	}

	if( Top < 0 )
	{
		SetY( float(GetHeight()/2) );
		SetVelY( 0.0f );
	}
	else if( Bottom > pGame->GetScreenHeight())
	{
		SetY( float(pGame->GetScreenHeight() - float(GetHeight()/2)));
		SetVelY( 0.0f );
	}

	//CEntity::Update(pElapsedTime);

	//SetVelX(0);
	//SetVelY(0);
}

void CPlayer::Render(void)
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();



	float ToDrawX = int(GetX()) - float(GetWidth()/2);
	float ToDrawY = int(GetY()) - float(GetHeight()/2);

	RECT ToDraw = {0,0,88,88};

	

	m_TextureGuy->Draw( (CEntity::GetImageID()), ToDrawX, ToDrawY, 1.0f, 1.0f );

	//int ToDrawX = int(GetX()) - int(GetWidth()/2);
	//int ToDrawY = int(GetY()) - int(GetHeight()/2);

	int TestingX = (int)GetX();
	int TestingY = (int)GetY();

//	RECT ToDraw = {0,0,128,128};
	
	if(m_bUBlu)
	{
		m_TextureGuy->Draw( (CEntity::GetImageID()), ToDrawX, ToDrawY, 1.0f, 1.0f, 0, 0, 0, 0, D3DCOLOR_XRGB(0,0,255) );
	}
	else
	{
		m_TextureGuy->Draw( (CEntity::GetImageID()), ToDrawX, ToDrawY, 1.0f, 1.0f, 0, 0, 0, 0, D3DCOLOR_XRGB(255,0,0) );
	}
}

void CPlayer::Input( void )
{
		// Basic Movement
	if( m_InputGuy->KeyDown( DIK_UP ) || m_InputGuy->KeyDown( DIK_W )
		|| this->m_InputGuy->JoystickDPadDown(DIR_UP, 0) || this->m_InputGuy->JoystickGetLStickDirDown(DIR_UP, 0))
	{
		SetVelY(-200);
	}
	else if( m_InputGuy->KeyDown( DIK_DOWN ) || m_InputGuy->KeyDown( DIK_S )
		|| this->m_InputGuy->JoystickDPadDown(DIR_DOWN, 0) || this->m_InputGuy->JoystickGetLStickDirDown(DIR_DOWN, 0))
	{
		SetVelY(200);
	}
	else
		this->SetVelY(0.0f);

	if( m_InputGuy->KeyDown( DIK_LEFT )|| m_InputGuy->KeyDown( DIK_A )
		|| this->m_InputGuy->JoystickDPadDown(DIR_LEFT, 0) || this->m_InputGuy->JoystickGetLStickDirDown(DIR_LEFT, 0))
	{
		SetVelX(-200);
	}
	else if( m_InputGuy->KeyDown( DIK_RIGHT ) || m_InputGuy->KeyDown( DIK_D )
		|| this->m_InputGuy->JoystickDPadDown(DIR_RIGHT, 0) || this->m_InputGuy->JoystickGetLStickDirDown(DIR_RIGHT, 0))
	{
		SetVelX(200);
		CCamera::GetInstance()->SetSpeedModifier(-100.0f);
	}
	else
	{
		this->SetVelX(0.0);
		CCamera::GetInstance()->SetSpeedModifier(0.0f);
	}


	if( m_InputGuy->KeyPressed( DIK_J ) || this->m_InputGuy->JoystickButtonPressed(1, 0))
	{
		if( m_nMP - GetAbilityA()->GetManaCost() >= 0 )
		{
			m_nMP -= GetAbilityA()->GetManaCost();
			CCreateAbilityMessage* pMsg_EX1 = new CCreateAbilityMessage(this);
			m_Messenger->SendMsg( pMsg_EX1 );
			pMsg_EX1 = nullptr;
		}
	}

		if( m_InputGuy->KeyPressed( DIK_K ) )
		{
			m_AudioGuy->SFXPlaySound(m_nAtttackSFXID,false);

			CAttackMessage* pMsg_EX1 = new CAttackMessage(this);
			m_Messenger->SendMsg( pMsg_EX1 );
			pMsg_EX1 = nullptr;

			//RECT rOverlap;
			//	
			//RECT AttackRect = {this->GetX( ) + this->GetWidth(),
			//										this->GetY( ) + this->GetHeight(),
			//										this->GetX( ) +  this->GetWidth( ) + 100,
			//										this->GetY( ) + this->GetHeight() + 100}


			//BOOL bColliding = IntersectRect( &rOverlap, &AttackRect, &TestingOther );
		}

	if( m_InputGuy->KeyPressed( DIK_SPACE ) || this->m_InputGuy->JoystickButtonPressed(5, 0))
	{
		if(m_bUBlu)
		{
			CEventSystem::GetInstance()->SendEvent( "Go Red", this );
		}
		else
		{
			CEventSystem::GetInstance()->SendEvent( "Go Blue", this );
		}
	}
}

void CPlayer::HandleEvent( CEvent* pEvent )
{
	if( pEvent->GetEventID() == "Go Blue")
		m_bUBlu = true;

	if(pEvent->GetEventID() == "Go Red")
		m_bUBlu = false;
}
